package com.chixing.service.impl;

import com.chixing.mapper.SecKillMapper;
import com.chixing.pojo.SecKill;
import com.chixing.pojo.SecKillExample;
import com.chixing.service.SeckillService;
import com.chixing.util.DateUtil;
import com.chixing.util.ServerResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class SeckillServiceImpl implements SeckillService {

  @Autowired
  private SecKillMapper secKillMapper;

  @Override
  public ServerResult getSecKillPro(String startTime) {

    Date date = DateUtil.stringToDate(startTime, DateUtil.DATE_FORMAT_YMSHM);
//    Date date = new Date(startTime);
    //查询秒杀商品表中，对应时间的秒杀商品。
    SecKillExample secKillExample = new SecKillExample();
    SecKillExample.Criteria criteria = secKillExample.createCriteria();
    criteria.andSeckillStarttimeEqualTo(date);

    List<SecKill> secKills = secKillMapper.selectByExample(secKillExample);

    if (secKills.size() != 0) {
      return ServerResult.getSuccess(secKills);
    } else {
      return ServerResult.getFail(null);
    }
  }

  @Override
  public List<SecKill> getAllSecKillPro() {
    List<SecKill> secKills = secKillMapper.selectByExample(null);
    if (secKills.size() != 0) {
      return secKills;
    }
    return null;
  }
}
